package com.cncytech.uiautomator_demo.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class FileHelper {
    String filepath = "";

    public FileHelper(String filepath) {
        this.filepath = filepath;
    }

    public FileHelper() {
    }

    public File createAppFile(String fileName) {
        File txtFile = new File(fileName);
        try {
            if (!txtFile.exists()) {
                txtFile.createNewFile();
            } else {
                txtFile.delete();
                txtFile.createNewFile();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return txtFile;

    }

    public File createResultFile(String fileName) {
        // create file to write test result
        File file = new File(File.separator + "data" + File.separator + "local"
                + File.separator + "tmp" + File.separator + "uiauto"
                + File.separator);
        if (!file.exists()) {
            file.mkdir();
        }

        File txtFile = new File(File.separator + "data" + File.separator
                + "local" + File.separator + "tmp" + File.separator + "uiauto"
                + File.separator + fileName);
        try {
            if (!txtFile.exists()) {
                txtFile.createNewFile();
            } else {
                txtFile.delete();
                txtFile.createNewFile();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return txtFile;
    }

    public void writeDataToFile(File fileName, String data) {
        BufferedWriter fw = null;
        try {
            fw = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(fileName, true), "UTF-8"));
            fw.append(data);
            fw.newLine();
            fw.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public String readMailTemplate() {
        int len = 0;
        StringBuffer str = new StringBuffer("");
        File file = new File(filepath);
        System.out.println(file.getAbsolutePath());
        try {
            FileInputStream is = new FileInputStream(file);
            InputStreamReader isr = new InputStreamReader(is,"UTF-8");
            BufferedReader in = new BufferedReader(isr);
            String line = null;
            while ((line = in.readLine()) != null) {
                if (len != 0) // 处理换行符的问题
                {
                    str.append(System.getProperty("line.separator") + line);

                } else {
                    str.append(line);
                }
                len++;
            }
            in.close();
            is.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }
        return str.toString();
    }

    //push file log
    public static void saveToFile(String text, String path, boolean isClose) {
        File file = new File(path);
        BufferedWriter bf = null;
        try {
            FileOutputStream outputStream = new FileOutputStream(file, true);
            OutputStreamWriter outWriter = new OutputStreamWriter(outputStream);
            bf = new BufferedWriter(outWriter);
            bf.append(text);
            bf.newLine();
            bf.flush();

            if (isClose) {
                bf.close();
            }
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
